// 对话相关 API

import request from './index';
import type { SendMessageRequest, SendMessageResponse, CreateSessionResponse } from '@/types/chat';
import type { ApiResponse, GetSessionsResponse, GetSessionDetailResponse } from '@/types/api';
import { 
  mockSendMessage, 
  mockGetSessions, 
  mockGetSessionDetail, 
  mockCreateSession,
  mockDeleteSession 
} from '@/mock/chat';

const useMock = import.meta.env.VITE_USE_MOCK === 'true';

/**
 * 发送消息
 */
export const sendMessage = async (data: SendMessageRequest): Promise<ApiResponse<SendMessageResponse>> => {
  if (useMock) {
    return mockSendMessage(data);
  }
  return request.post('/chat/send', data);
};

/**
 * 获取会话列表
 */
export const getSessions = async (): Promise<ApiResponse<GetSessionsResponse>> => {
  if (useMock) {
    return mockGetSessions();
  }
  return request.get('/chat/sessions');
};

/**
 * 获取会话详情
 */
export const getSessionDetail = async (sessionId: string): Promise<ApiResponse<GetSessionDetailResponse>> => {
  if (useMock) {
    return mockGetSessionDetail(sessionId);
  }
  return request.get(`/chat/session/${sessionId}`);
};

/**
 * 创建新会话
 */
export const createSession = async (title?: string): Promise<ApiResponse<CreateSessionResponse>> => {
  if (useMock) {
    return mockCreateSession(title);
  }
  return request.post('/chat/session', { title });
};

/**
 * 删除会话
 */
export const deleteSession = async (sessionId: string): Promise<ApiResponse<null>> => {
  if (useMock) {
    return mockDeleteSession(sessionId);
  }
  return request.delete(`/chat/session/${sessionId}`);
};